package com.ymatou.userservice.testcase;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
import com.ymatou.auth.parameter.SecurityQuestion;
import com.ymatou.auth.service.LoginAuthCall;
import com.ymatou.userservice.parameter.LoginBillsRequestDtoBean;
import com.ymatou.userservice.parameter.UnBindMobileRequestDtoBean;
import com.ymatou.userservice.service.LoginBillsRequestDtoCall;
import com.ymatou.userservice.service.UnBindMobileRequestDtoCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AuthCallService;
import com.ymttest.business.service.UserCallService;
import com.ymttest.database.model.YmtMobilePhoneAccount;
import com.ymttest.database.mongo.ULC201406Bean;
import com.ymttest.database.mongo.ULC201406Wapper;
import com.ymttest.database.sqlwapper.UserLogoWapper;
import com.ymttest.database.sqlwapper.YmtMobilePhoneAccountWapper;
import com.ymttest.database.sqlwapper.YmtUsersWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.BillsActionType;
import com.ymttest.utils.constant.ECode;
/**
*解绑手机
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_UnBindMobileRequestDto{
	private static UnBindMobileRequestDtoBean UnBindMobileRequestDtoBean;
	private static UnBindMobileRequestDtoCall UnBindMobileRequestDtoCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("解绑手机");
	}

	@Before
	public void caseUp() {
		UnBindMobileRequestDtoBean=new UnBindMobileRequestDtoBean();
		UnBindMobileRequestDtoCall=new UnBindMobileRequestDtoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(UnBindMobileRequestDtoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UnBindMobileRequestDto_001() {
		Logger.start(true,"解绑手机成功");
		try {
			Map m=UserCallService.registerByMobile();
			Integer uid=Integer.valueOf(m.get("userid").toString());
			String phone=m.get("phone").toString();
			UnBindMobileRequestDtoBean.setMobile(phone);
			UnBindMobileRequestDtoBean.setUserId(uid);
			UnBindMobileRequestDtoCall.setData(UnBindMobileRequestDtoBean);
			UnBindMobileRequestDtoCall.callService();			
			Logger.verifyEquals("200", UnBindMobileRequestDtoCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals("true", UnBindMobileRequestDtoCall.getString("Success"), "验证Success");
			Thread.sleep(1000);
			//ulc2016 ymt_users Ymt_MobilePhoneAccount userinfo_updaterecord
			ULC201406Bean ub = new ULC201406Wapper().getULCInfoByUid(uid);
			Logger.verifyEquals("",ub.getlBills().getMobile(),"验证mobile在ULC201406解绑");
			
			YmtMobilePhoneAccount mp = new YmtMobilePhoneAccountWapper().getAccountInfo(uid);
			Logger.verifyEquals(null,mp,"验证mobile在YmtMobilePhoneAccount解绑(没有记录)");
			
			Map uum=new YmtUsersWapper().selectUserinfoUpdaterecord(uid).get(0);
			Logger.verifyEquals(phone,uum.get("NewBizValue"),"验证UserinfoUpdaterecord解绑记录手机号NewBizValue");
			Logger.verifyEquals(phone,uum.get("OldBizValue"),"验证UserinfoUpdaterecord解绑记录手机号OldBizValue");
			Logger.verifyEquals("4",uum.get("UserBizType").toString(),"验证UserinfoUpdaterecord解绑记录手机号UserBizType");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_UnBindMobileRequestDto_002() {
		Logger.start(false,"不输入参数userid,失败");
		try {
			Map m=UserCallService.registerByMobile();
			UnBindMobileRequestDtoBean.setMobile(m.get("phone").toString());
//			UnBindMobileRequestDtoBean.setUserId(Integer.valueOf(m.get("userid").toString()));
			UnBindMobileRequestDtoCall.setData(UnBindMobileRequestDtoBean);
			UnBindMobileRequestDtoCall.callService();			
			Logger.verifyEquals("400", UnBindMobileRequestDtoCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals("false", UnBindMobileRequestDtoCall.getString("Success"), "验证Success");
			Logger.verifyEquals("用户Id非法", UnBindMobileRequestDtoCall.getString("LastErrorMessage"), "验证LastErrorMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_UnBindMobileRequestDto_003() {
		Logger.start(false,"不输入参数moblie,失败");
		try {
			Map m=UserCallService.registerByMobile();
//			UnBindMobileRequestDtoBean.setMobile(m.get("phone").toString());
			UnBindMobileRequestDtoBean.setUserId(Integer.valueOf(m.get("userid").toString()));
			UnBindMobileRequestDtoCall.setData(UnBindMobileRequestDtoBean);
			UnBindMobileRequestDtoCall.callService();			
			Logger.verifyEquals("400", UnBindMobileRequestDtoCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals("false", UnBindMobileRequestDtoCall.getString("Success"), "验证Success");
			Logger.verifyEquals("手机号不能为空", UnBindMobileRequestDtoCall.getString("LastErrorMessage"), "验证LastErrorMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_UnBindMobileRequestDto_004() {
		Logger.start(false,"uid和手机号不匹配,失败");
		try {
			Map m=UserCallService.registerByMobile();
			UnBindMobileRequestDtoBean.setMobile(m.get("phone").toString());
			UnBindMobileRequestDtoBean.setUserId(Integer.valueOf(m.get("userid").toString())-1);
			UnBindMobileRequestDtoCall.setData(UnBindMobileRequestDtoBean);
			UnBindMobileRequestDtoCall.callService();			
			Logger.verifyEquals("700", UnBindMobileRequestDtoCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals("false", UnBindMobileRequestDtoCall.getString("Success"), "验证Success");
			Logger.verifyEquals("手机与用户不匹配", UnBindMobileRequestDtoCall.getString("LastErrorMessage"), "验证LastErrorMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_UnBindMobileRequestDto_005() {
		Logger.start(false,"userid不存在，失败");
		try {
			Map m=UserCallService.registerByMobile();
			UnBindMobileRequestDtoBean.setMobile(m.get("phone").toString());
			UnBindMobileRequestDtoBean.setUserId(999999999);
			UnBindMobileRequestDtoCall.setData(UnBindMobileRequestDtoBean);
			UnBindMobileRequestDtoCall.callService();			
			Logger.verifyEquals("500", UnBindMobileRequestDtoCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals("false", UnBindMobileRequestDtoCall.getString("Success"), "验证Success");
			Logger.verifyEquals("找不到用户", UnBindMobileRequestDtoCall.getString("LastErrorMessage"), "验证LastErrorMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
//	@Ignore
	public void Tc_UnBindMobileRequestDto_006() {
		Logger.start(false,"手机绑定记录不存在,失败");
		try {
			Map m=UserCallService.registerByMobile();
			UnBindMobileRequestDtoBean.setMobile(m.get("phone").toString());
			UnBindMobileRequestDtoBean.setUserId(Integer.valueOf(m.get("userid").toString()));
			UnBindMobileRequestDtoCall.setData(UnBindMobileRequestDtoBean);
			UnBindMobileRequestDtoCall.callService();
			UnBindMobileRequestDtoCall.callService();	
			Logger.verifyEquals("600", UnBindMobileRequestDtoCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals("false", UnBindMobileRequestDtoCall.getString("Success"), "验证Success");
			Logger.verifyEquals("手机绑定记录不存在", UnBindMobileRequestDtoCall.getString("LastErrorMessage"), "验证LastErrorMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UnBindMobileRequestDto_007() {
		Logger.start(true,"，注册成功后从Ymt_MobilePhoneAccount中删除记录再解绑成功");
		try {
			Map m=UserCallService.registerByMobile();
			Integer uid=Integer.valueOf(m.get("userid").toString());
			String phone=m.get("phone").toString();
			new YmtMobilePhoneAccountWapper().deleteByUid(Integer.valueOf(m.get("userid").toString()));
			UnBindMobileRequestDtoBean.setMobile(m.get("phone").toString());
			UnBindMobileRequestDtoBean.setUserId(Integer.valueOf(m.get("userid").toString()));
			UnBindMobileRequestDtoCall.setData(UnBindMobileRequestDtoBean);
			UnBindMobileRequestDtoCall.callService();			
			Logger.verifyEquals("200", UnBindMobileRequestDtoCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals("true", UnBindMobileRequestDtoCall.getString("Success"), "验证Success");
			//TODO 数据验证
			ULC201406Bean ub = new ULC201406Wapper().getULCInfoByUid(uid);
			Logger.verifyEquals("",ub.getlBills().getMobile(),"验证mobile在ULC201406解绑");
			
			YmtMobilePhoneAccount mp = new YmtMobilePhoneAccountWapper().getAccountInfo(uid);
			Logger.verifyEquals(null,mp,"验证mobile在YmtMobilePhoneAccount解绑(没有记录)");
			
			Map uum=new YmtUsersWapper().selectUserinfoUpdaterecord(uid).get(0);
			Logger.verifyEquals(phone,uum.get("NewBizValue"),"验证UserinfoUpdaterecord解绑记录手机号NewBizValue");
			Logger.verifyEquals(phone,uum.get("OldBizValue"),"验证UserinfoUpdaterecord解绑记录手机号OldBizValue");
			Logger.verifyEquals("4",uum.get("UserBizType").toString(),"验证UserinfoUpdaterecord解绑记录手机号UserBizType");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UnBindMobileRequestDto_008() {
		Logger.start(true,"解绑手机再绑定成功");
		try {
			Map m=UserCallService.registerByMobile();
			Integer uid=Integer.valueOf(m.get("userid").toString());
			String phone=m.get("phone").toString();
			UnBindMobileRequestDtoBean.setMobile(phone);
			UnBindMobileRequestDtoBean.setUserId(uid);
			UnBindMobileRequestDtoCall.setData(UnBindMobileRequestDtoBean);
			UnBindMobileRequestDtoCall.callService();			
			Logger.verifyEquals("200", UnBindMobileRequestDtoCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals("true", UnBindMobileRequestDtoCall.getString("Success"), "验证Success");
			Thread.sleep(1000);
			//ulc2016 ymt_users Ymt_MobilePhoneAccount userinfo_updaterecord
			ULC201406Bean ub = new ULC201406Wapper().getULCInfoByUid(uid);
			Logger.verifyEquals("",ub.getlBills().getMobile(),"验证mobile在ULC201406解绑");
			
			YmtMobilePhoneAccount mp = new YmtMobilePhoneAccountWapper().getAccountInfo(uid);
			Logger.verifyEquals(null,mp,"验证mobile在YmtMobilePhoneAccount解绑(没有记录)");
			
			Map uum=new YmtUsersWapper().selectUserinfoUpdaterecord(uid).get(0);
			Logger.verifyEquals(phone,uum.get("NewBizValue"),"验证UserinfoUpdaterecord解绑记录手机号NewBizValue");
			Logger.verifyEquals(phone,uum.get("OldBizValue"),"验证UserinfoUpdaterecord解绑记录手机号OldBizValue");
			Logger.verifyEquals("4",uum.get("UserBizType").toString(),"验证UserinfoUpdaterecord解绑记录手机号UserBizType");
			Logger.comment("解绑完成后再次绑定手机");
			LoginBillsRequestDtoBean lbb=new LoginBillsRequestDtoBean();
			LoginBillsRequestDtoCall lbc=new LoginBillsRequestDtoCall();
			phone=TestDataManager.getPhoneNum();
			lbb.setMobile(phone);
			lbb.setUserId(uid);
			lbb.setAction(BillsActionType.BINDMOBILE);
			lbb.setLoginPassword("123456");
			lbb.setActionSource("mainsite");
			lbb.setLoginId(m.get("username").toString());
			lbc.setData(lbb);
			lbc.callService();
			Logger.verifyEquals(ECode.SUCCESS, lbc.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("true", lbc.getString("Success"), "验证操作成功");
			Logger.verifyEquals("ok", lbc.getString("SuccessMessage"), "验证Msg信息");
			Logger.verifyEquals("Success", lbc.getString("Result"), "验证Resul信息");
			
			ULC201406Bean ub2 = new ULC201406Wapper().getULCInfoByUid(uid);
			Logger.verifyEquals(phone,ub2.getlBills().getMobile(),"验证mobile在ULC201406解绑");
			
			YmtMobilePhoneAccount mp2 = new YmtMobilePhoneAccountWapper().getAccountInfo(uid);
			Logger.verifyEquals(phone,mp2.getSaccountname(),"验证mobile在YmtMobilePhoneAccount解绑(没有记录)");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}